﻿using Core.Configuration.Api;
using Core.Configuration.Config;
using IconYuan.Api.Services;
using IconYuan.Manager.Orders;
using IconYuan.Manager.Products;
using IconYuan.Manager.Stores;
using IconYuan.Manager.Users;
using IconYuan.Model.DB;
using IconYuan.Model.DTO.Orders;
using IconYuan.Model.DTO.Products;
using IconYuan.Model.Entity.Orders;
using IconYuan.Model.Entity.Stores;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace IconYuan.Api.Controllers.Exchanges
{
    /// <summary>
    /// 兑换操作(商品列表、兑换记录)
    /// </summary>
    public class ExchangeController : ApiBase
    {
        readonly UserManager userSvc;
        readonly PointLogManager pointLogSvc;
        readonly GoodsManager goodsSvc;
        readonly OrderManager orderSvc;
        readonly OrderGoodsManager orderGoodsSvc;
        readonly ShopManager shopSvc;
        public ExchangeController()
        {
            userSvc = new UserManager();
            pointLogSvc = new PointLogManager();
            goodsSvc = new GoodsManager();
            orderSvc = new OrderManager();
            orderGoodsSvc = new OrderGoodsManager();
            shopSvc = new ShopManager();
        }

        /// <summary>
        /// 兑换商品列表
        /// </summary>
        /// <param name="shop_id"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<GoodsBaseDto>> GoodsList(int shop_id)
        {
            //兑换商品分类
            var category_ids = GlobalConfig.ExchangeCategoryId.Split(',')
                    .Select(x => Convert.ToInt32(x)).ToList();

            var list = await goodsSvc.GetAllAsync(shop_id, category_ids);

            return GoodsServices.ToGoods(list);
        }

        /// <summary>
        /// 兑换订单记录
        /// </summary>
        /// <param name="user_id">用户id: 0 所有会员</param>
        /// <param name="status">状态: 0所有订单, 30 待兑换, 70 已兑换</param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageList<OrderDto>> OrderList(int user_id, int status = 0, int page = 1)
        {
            var expGoods = Expressionable.Create<OrderGoods>();
            var expShop = Expressionable.Create<Shop>();
            var exp = Expressionable.Create<Order>();
            exp.And(x => x.jump == 3);
            exp.AndIF(user_id > 0, x => x.user_id == user_id);
            if (status > 0)
                exp.And(x => x.order_status == status);
            else
            {
                var statusList = new List<int>() { 30, 70 };
                exp.And(x => statusList.Contains(x.order_status));
            }

            //兑换订单列表
            var result = await orderSvc.GetPageListAsync(exp.ToExpression(), y => y.order_id, OrderByType.Desc, page);
            result.data.ForEach(x =>
            {
                expGoods.Or(y => y.order_id == x.order_id);
                expShop.Or(y => y.shop_id == x.shop_id);
            });
            //门店列表
            var shopList = await shopSvc.GetListAsync(expShop.ToExpression());
            //商品列表
            var goodsList = await orderGoodsSvc.GetListAsync(expGoods.ToExpression(), y => y.order_id, OrderByType.Desc);
            foreach (var goods in goodsList)
            {
                goods.goods_image = $"{GlobalConfig.CdnDomain}{goods.goods_image}";
            }
            
            var list = result.data.Select(x => OrdersServices.ToOrderDto(x, goodsList, shopList)).ToList();
            return new PageList<OrderDto>()
            {
                data = list,
                current_page = result.current_page,
                last_page = result.last_page,
                per_page = result.per_page,
                total = result.total,
            };
        }
    }
}